//
//  HBHForumViewController.m
//  HubeiHotel
//
//  Created by 张露杰 on 17/11/13.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "HBHForumViewController.h"
#import "ForumCell.h"
#import "ForumDetailViewController.h"
#import "FoeumSearchViewController.h"
#import "ForumFaBu.h"
@interface HBHForumViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;

@property (nonatomic, copy) NSString *isHot;
@property (nonatomic, retain) NSMutableArray *datas;
@property (nonatomic) NSInteger pageNo;

@end

@implementation HBHForumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    [self getList];
}

-(void)creatView
{
    _isHot = @"0";
    _datas = [NSMutableArray new];
    _pageNo = 1;
    [self initLeftBarButtonWithImage:@"Foumersearch_white"];
    [self initRightBarButtonWithTitle:@"发帖" andTitleColor:[UIColor whiteColor] andFont:16*screenW_6];
    self.navigationItem.titleView = [self segmentControlWithItems:@[@"关注", @"热门"]];
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV//创建视图
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH - 64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 180*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
        //刷新
        _tableV.mj_header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
            _pageNo = 1;
            [_datas removeAllObjects];
            [_tableV.mj_footer resetNoMoreData];
            [self getList];
        }];
        //加载
        _tableV.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
            _pageNo ++;
            [self getList];
        }];
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _datas.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10*screenW_6;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ForumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ForumCell"];
    if (cell == nil) {
        cell = [[ForumCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ForumCell"];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(ForumCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell loadCellWithData:_datas[indexPath.section]];
    __weak typeof(self) weakSelf = self;
    cell.commentTapBlock = ^(NSInteger index){
        if (index == 0) {
            
        }else if (index == 1){
            NSMutableDictionary *post = weakSelf.datas[indexPath.section];
            if ([post[@"isClick"] boolValue]) {
                [TomamaUI creatAlertWithTarget:weakSelf message:@"已点过赞" timeInterval:2];
            }else{
                [post setObject:@"1" forKey:@"isClick"];
                [post setObject:[NSString stringWithFormat:@"%ld",[post[@"clickNum"] integerValue] + 1>0?[post[@"clickNum"] integerValue] + 1:0] forKey:@"clickNum"];
                [weakSelf.datas replaceObjectAtIndex:indexPath.section withObject:post];
                [tableView reloadData];
                [weakSelf clickPostWithIndex:indexPath.section];
            }
        }
    };
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h;
    h += [TomamaUI getStringHeightWith:_datas[indexPath.section][@"content"] andWeith:SCREENW - 30*screenW_6 andFont:[UIFont systemFontOfSize:14*screenW_6]];
    NSArray *imags = _datas[indexPath.section][@"imgList"];
    if (imags.count) {
        h += 175*screenW_6;
    }
    return h + 105*screenW_6;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row >= _datas.count) {
        return;
    }
    ForumDetailViewController *vc = [ForumDetailViewController new];
    vc.postId = _datas[indexPath.section][@"id"];
    __weak typeof(self) weakSelf = self;
    vc.deleteBlcok = ^(NSString *postId){
        [weakSelf.datas removeObjectAtIndex:indexPath.row];
        [tableView reloadData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - request
-(void)getList
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/post/postList" Dic:@{@"jdUserId":[Storage getToken],@"isHot":_isHot,@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],@"pageSize":@"10"} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    if (_datas.count < [responseDic[@"count"] integerValue]) {
                        [_datas addObjectsFromArray:responseDic[@"data"]];
                    }
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
                [_tableV reloadData];
                if ([_tableV.mj_header isRefreshing]) {
                    [_tableV.mj_header endRefreshing];
                }
                if ([_tableV.mj_footer isRefreshing]) {
                    if (_datas.count < [responseDic[@"count"] integerValue]) {
                        [_tableV.mj_footer endRefreshing];
                    }else{
                        [_tableV.mj_footer endRefreshingWithNoMoreData];
                    }
                }
            });
        }];
    });
}

-(void)clickPostWithIndex:(NSInteger)index
{
    NSMutableDictionary *post = [NSMutableDictionary dictionaryWithDictionary:_datas[index]];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/post/clickPost" Dic:@{@"post.id":post[@"id"],@"user.id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([responseDic[@"success"] boolValue]) {
                    
                }else{
                    [post setObject:@"0" forKey:@"isClick"];
                    [post setObject:[NSString stringWithFormat:@"%ld",[post[@"clickNum"] integerValue] - 1>0?[post[@"clickNum"] integerValue] - 1:0] forKey:@"clickNum"];
                    [_datas replaceObjectAtIndex:index withObject:post];
                    [_tableV reloadData];
                }
            });
        }];
    });
}


#pragma mark - segmentControlClicked
-(void)segmentControlClicked:(UISegmentedControl *)segmentControl
{
    if (segmentControl.selectedSegmentIndex == 0 && [_isHot isEqualToString:@"1"]) {
        _isHot = @"0";
        _pageNo = 1;
        [_datas removeAllObjects];
        [HudHelper showProgressHudWithMessage:@"" toView:self.view];
        [self getList];
    }else if(segmentControl.selectedSegmentIndex == 1 && [_isHot isEqualToString:@"0"]){
        _isHot = @"1";
        [_datas removeAllObjects];
        _pageNo = 1;
        [HudHelper showProgressHudWithMessage:@"" toView:self.view];
        [self getList];
    }
}


-(void)onTapLeftBarButton{//搜索
    FoeumSearchViewController *vc = [FoeumSearchViewController new];
    vc.isHot = _isHot;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)onTapRightBarButton{//发帖
    [self.navigationController pushViewController:[ForumFaBu new] animated:YES];
}

@end
